package cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc;

import cn.edu.xmu.oomall.liquidation.microservice.CustomerService;
import cn.edu.xmu.oomall.liquidation.microservice.ShareService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.LiquidationShareRetVo;
import cn.edu.xmu.oomall.liquidation.model.bo.ExpenditureItem;
import cn.edu.xmu.oomall.liquidation.util.GetBeanUtil;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import cn.edu.xmu.oomall.liquidation.microservice.OnSaleService;
import cn.edu.xmu.oomall.liquidation.microservice.ShareActivityService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.OnSaleVo;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Setter
public class PointLiquidationItem extends BaseLiquidationItem {
    private ShareActivityService shareActivityService = GetBeanUtil.getBean(ShareActivityService.class);

    private OnSaleService onSaleService = GetBeanUtil.getBean(OnSaleService.class);

    private ShareService shareService = GetBeanUtil.getBean(ShareService.class);

    private CustomerService customerService = GetBeanUtil.getBean(CustomerService.class);

    private Logger logger = LoggerFactory.getLogger(PointLiquidationItem.class);

    public PointLiquidationItem(String dbFieldName) {
        super(dbFieldName);
    }

    @Override
    public Long calcAmount(CalcInfo calcInfo) {
        if (calcInfo instanceof PaymentCalcInfo) {
            return calcPayment((PaymentCalcInfo) calcInfo);
        }
        else if (calcInfo instanceof RefundCalcInfo) {
            return calcRefund((RefundCalcInfo) calcInfo);
        }
        return null;
    }

    private Long calcRefund(RefundCalcInfo refundCalcInfo) {
        //查分享者的分享表，看现在已经分享了几件
        ExpenditureItem expenditureItem = refundCalcInfo.getExpenditureItem();
        InternalReturnObject<Long> customerSharesQuantityObj = shareService.getSharesQuantity(refundCalcInfo.getSharerId());
        Long customerSharesQuantity = customerSharesQuantityObj.getData();
        //用CustomerQuantity-quantity=oldQuantity,再用oldQuantity,quantity去算返点
        Long oldQuantity = customerSharesQuantity - refundCalcInfo.getQuantity();
        //拿到分享活动id
        OnSaleVo onSaleVo = onSaleService.getOnsaleById(refundCalcInfo.getOnsaleId()).getData();
        //计算扣除的返点
        //Todo price是负数,要不要用-price去掉分享活动的计算返点
        Long price = refundCalcInfo.getExpenditureItem().getAmount();
        Long point = -shareActivityService.calculatePoint(onSaleVo.getShareActId(), -price, oldQuantity, refundCalcInfo.getQuantity()).getData();
        //扣除分享人返点
        customerService.addCustomerPoint(expenditureItem.getSharerId(), point);
        //设置返点
        expenditureItem.setPoint(point);
        //设置商铺收入
        expenditureItem.setShopRevenue(expenditureItem.getShopRevenue()-point);
        return point;
    }

    private Long calcPayment(PaymentCalcInfo paymentCalcInfo) {
        OnSaleVo onSaleVo = onSaleService.getOnsaleById(paymentCalcInfo.getOnsaleId()).getData();

        //拿出分享成功列表
        List<LiquidationShareRetVo> liquidationShareRetVoList = shareService.getBesharedList(paymentCalcInfo.getProductId(), paymentCalcInfo.getOnsaleId(), paymentCalcInfo.getCustomerId()).getData();
        LiquidationShareRetVo firstSuccessShare = liquidationShareRetVoList.get(0);
        //设置第一个人为已经清算
        shareService.updateBeSharedState(firstSuccessShare.getId(),(byte)1);
        //其他人为失效
        for (int i = 1; i < liquidationShareRetVoList.size(); i++) {
            shareService.updateBeSharedState(liquidationShareRetVoList.get(i).getId(),(byte)2);
        }
        //第一个人已经分享的件数
        Long oldQuantity = shareService.getSharesQuantity(firstSuccessShare.getShareId()).getData();
        //拿出分享活动id，价格，分享者已分享数量,售出数量,去计算返点

        Long price = paymentCalcInfo.getRevenueItem().getAmount();
        Long point = shareActivityService.calculatePoint(onSaleVo.getShareActId(), price, oldQuantity, paymentCalcInfo.getQuantity()).getData();
        //分享者增加已分享件数
        shareService.addSharesQuantity(firstSuccessShare.getShareId(), paymentCalcInfo.getQuantity());

        //增加分享者返点
        Long firstSuccessShareSharerId = firstSuccessShare.getSharerId();
        customerService.addCustomerPoint(firstSuccessShareSharerId,point);

        //设置进账单
        //设置分享者id
        paymentCalcInfo.getRevenueItem().setSharerId(firstSuccessShareSharerId);
        //设置返点
        paymentCalcInfo.getRevenueItem().setPoint(point);
        //修改商铺收入
        Long shopRevenue = paymentCalcInfo.getRevenueItem().getShopRevenue();
        paymentCalcInfo.getRevenueItem().setShopRevenue(shopRevenue-point);

        return point;
    }
}
